--[[/**
-- * 
死亡时触发,西泽死亡后回到丽姬娅身旁接受治疗，以每秒8%的速度恢复生命，当生命恢复到100%后将得到重生（恢复期间无法被选中）。
-- *
-- */]]

---@class BuffEffect613100 : BaseBuffEffect
BuffEffect613100 = ClientFight.CreateClass("BuffEffect613100", ClientFight.BaseBuffEffect)
local t = BuffEffect613100

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    t:removeDebuff(fight, buff);

    local buffTarget = buff.target;
    if (buffTarget:isSummons()) then
        return IBuffEffect.FAIL;
    end
    --判断下是否还有队友存活
    local fighters = buffTarget.fightTeam.fighters;
    local hasTeamMem = false;
    for k, fighter in pairs(fighters) do
        if (fighter:isDie()) then
            goto continue;
        end
        if (fighter:isSummons()) then
            goto continue;
        end
        if (fighter == buffTarget) then
            goto continue;
        end
        if (fighter == buffTarget.puppet) then
            goto continue;
        end
        hasTeamMem = true;
        :: continue ::
        break ;
    end
    if (not hasTeamMem) then
        local list = buffTarget.fightTeam.alternates;
        if (list.isEmpty()) then
            --既没有队友,也没有支援
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buffTarget.fighterId .. " Buff"
                    .. buff.modelId .. " 死亡时 既没有队友,也没有支援");
            return IBuffEffect.FAIL;
        end
        --支援上场
        local site = buffTarget.site;
        site = site - 1;
        if (site < 1) then
            site = 5;
        end
        FighterManager.alternateReady(fight, buffTarget.teamIndex, site, 1);--上替补
    end
    local duelTarget = buffTarget.duelTarget;
    if (duelTarget ~= nil) then
        --强行解除决斗
        buffTarget.duelTarget = nil;
        duelTarget.duelTarget = nil;
    end
    buffTarget.prepareTarget = nil;
    -- 把血加回来
    DamageManager.setFighterHp(fight, buffTarget, buffTarget, nil, 0, 1,
            1, false, false, false);

    t:triggerSkill(fight, buffTarget, buffTarget:isSummons() and buffTarget or buffTarget.puppet, buff.buffBean.f_BuffEffectValue[1]);
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buffTarget.fighterId .. " Buff"
            .. buff.modelId .. " 触发死亡锁血并开始回到初始位置(触发一个冲锋技能)：");
    local puppet = buffTarget.puppet;
    if (puppet ~= nil and not buffTarget:isSummons()) then
        --			6111100
        t:puppetSHowSkill(fight, puppet, 6111100);
    end
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

function t:puppetSHowSkill(fight, puppet, skillId)
    local skill = SkillManager.createFightSkill(fight, skillId, puppet);
    local usingSkill = FightUsingSkill.New();
    usingSkill.skillUniqueId = fight.skillIds.andIncrement;
    usingSkill.fight = fight;
    usingSkill.fighter = puppet;
    usingSkill.target = puppet;
    usingSkill.skill = skill;
    usingSkill.parentSkill = usingSkill;
    usingSkill.speed = SkillManager.countSkillSpeed(usingSkill);
    local attack = ReportManager.attackToJsonString(fight, puppet, puppet, usingSkill, usingSkill.speed);
    table.insert(fight.actions, attack)
    FightManager.playActions(attack)
end

function t:removeDebuff(fight, buff)
    local target = buff.target;
    local remove = {};

    local overlay = Integer.MAX_VALUE;

    local iterator = clone( target.buffs);
    for key, entry in pairs(iterator) do
        if (overlay <= 0) then
            break ;
        end
        local _buff = entry;
        if (BuffTypeEnum.NEGATIVE == _buff.buffBean.f_BuffType) then
            if (_buff.overlay < overlay) then
                table.insert(remove, _buff);
                overlay = overlay - _buff.overlay;
            else
                _buff.overlay = buff.overlay - overlay;
                -- 重置效果
                BuffEffectManager.addBuffEffect(fight, _buff);
                local logOverlay = overlay;
                SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. target.fighterId
                        .. " Buff" .. buff.modelId .. " 移除Buff(" .. _buff.modelId .. ")" .. logOverlay);
                overlay = 0;
            end
        end
    end

    if (not remove.isEmpty()) then
        for k, fighterBuff in pairs(remove) do
            BuffManager.removeFighterBuff(fight, target, fighterBuff, true, buff.addParameters);
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. target.fighterId
                    .. " Buff" .. buff.modelId .. " 移除Buff(" .. fighterBuff.modelId .. ")");
        end
    end
end
t.New()